<?php

/**
 * This is the model class for table "data_users".
 *
 * The followings are the available columns in table 'data_users':
 * @property integer $id
 * @property string $fullname
 * @property string $email
 * @property integer $pid
 * @property string $username
 * @property string $password
 * @property integer $active
 */
class Users extends CActiveRecord
{
	// holds the password confirmation word
    public $repeat_password;
 
    //will hold the encrypted password for update actions.
    public $new_password;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'data_users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('fullname, username, email', 'required'),
			array('new_password, repeat_password', 'required', 'on' => 'create'),
//			array('password, password_repeat', 'allowEmpty'=>true, 'on' => 'update'),
			array('role, active', 'numerical', 'integerOnly'=>true),
			array('fullname, username', 'length', 'max'=>100),
			array('new_password, repeat_password', 'length', 'min'=>6),
			array('email', 'safe'),
            array('new_password', 'compare', 'compareAttribute'=>'repeat_password'),
            
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, fullname, email, pid, username, password, active', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return boolean validate user
	 */
	public function validatePassword($password, $username)
	{
	        return $this->hashPassword($password) === $this->password;
	}
	/**
	 * @return hashed value
	 */
	
	public function hashPassword($phrase, $salt = null)
	{
	        $key = 'Gf;B&yXL|beJUf-K*PPiU{wf|@9K9j5?d+YW}?VAZOS%e2c -:11ii<}ZM?PO!96';
	        if($salt == '')
	                $salt = substr(hash('sha512', $key), 0, 10);
	        else
	                $salt = substr($salt, 0, 10);
	        return hash('sha512', $salt . $key . $phrase);
	}
		
	
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'fullname' => 'Fullname',
			'email' => 'Email',
			'role' => 'Role',
			'username' => 'Username',
			'password' => 'Password',
			'new_password' => 'Password',
			'repeat_password' => 'Repeat Password',
			'active' => 'Active',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		$criteria->condition=' role !=0';
		$criteria->compare('id',$this->id);
		$criteria->compare('fullname',$this->fullname,true);
		$criteria->compare('email',$this->email,true);
//		$criteria->compare('pid',$this->pid);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('active',$this->active);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}